<template>
	<zh-form :span="24" ref="rolePageFrom" :model="state.form" :rules="state.rules">
		<zh-form-input prop="title" :required="true" label="标签名称" placeholder="请输入标签名称" v-model="state.form.title"></zh-form-input>
		<zh-form-select
			prop="labelType"
			label="标签类型"
			placeholder="请选择标签类型"
			:options="state.dictData.LABELTYPE"
			v-model="state.form.labelType"
		></zh-form-select>
		<zh-form-select
			prop="status"
			label="状态"
			placeholder="请选择状态"
			allowClear
			:options="state.dictData.STATUS"
			v-model="state.form.status"
		></zh-form-select>
		<zh-form-select
			prop="parentTitle"
			label="父标签"
			placeholder=""
			searchField="title"
			:searchParams="state.parentCodeParams"
			:default-fields="{ label: 'title', value: 'labelCode' }"
			:filterable="true"
			:remote="true"
			v-model="state.form.parentTitle"
			@change="onClick"
		></zh-form-select>
		<zh-form-input prop="remarks" type="textarea" :rows="4" label="备注" v-model="state.form.remarks"></zh-form-input>
	</zh-form>
</template>

<script lang="ts" setup name="LabelEditForm">
//------------------接收传入参数(组件)-------------------
const props = defineProps({
	labelInfo: {
		type: Object,
		default: () => {},
		required: false,
	},
	modelValue: Boolean,
});
const emit = defineEmits(['update:modelValue']);

//------------------定义子组件---------------------------
const rolePageFrom = ref<any>({});

//------------------内部变量定义-------------------------
const state = reactive({
	dictData: {} as any,
	form: {
		title: undefined,
		labelType: undefined,
		status: undefined,
		remarks: undefined,
		labelCode: undefined,
		parentCode: undefined,
		parentTitle: undefined,
	} as any,
	rules: {
		title: [{ required: true, message: '请输入用户名' }],
		labelType: [{ required: true, message: '请输入标签类型' }],
		status: [{ required: true, message: '请输入状态' }],
	},
	parentCodeParams: {
		tableName: 'dict_label',
		resultList: [],
		queryList: [],
	},
});

//------------------定义调用方法-------------------------
function checkForm() {
	let flag = true;
	rolePageFrom.value.validate((valid) => {
		if (valid) {
			flag = true;
		} else {
			flag = false;
		}
	});
	return flag;
}
function onClick(row) {
	state.form.parentCode = row.labelCode;
	state.form.parentTitle = row.title;
}
function saveLabel() {
	return new Promise((resolve, inject) => {
		if (checkForm()) {
			if (state.form.labelCode) {
				let params = state.form;
				$http
					.post($api.system.editLabel.url, params)
					.then(() => {
						console.log(1111);
						$messageBox.success('修改成功', '成功提示');
						emit('update:modelValue', false);
						console.log(props.modelValue);
						resolve(true);
					})
					.catch(() => {
						inject();
					});
			} else {
				let params = state.form;
				$http
					.post($api.system.addLabel.url, params)
					.then(() => {
						$messageBox.success('新增成功', '成功提示');
						emit('update:modelValue', false);
						resolve(true);
					})
					.catch(() => {
						inject();
					});
			}
		} else {
			$messageBox.warning('请输入必填项', '错误提示');
		}
	});
}

//------------------初始化------------------------------
// 数据初始化
const params = {
	dictFields: 'LABELTYPE,STATUS',
};
$http.post($api.system.getBatchDict.url, params).then((res) => {
	state.dictData = res.result;
});
if (props.labelInfo) {
	state.form = props.labelInfo;
	if (props.labelInfo.parentCode) {
		$http.post($api.system.getDictLabelList.url, { labelCode: props.labelInfo.parentCode }).then((res) => {
			state.form.parentTitle = res.result.list[0].title;
		});
	}
}
defineExpose({ saveLabel });
</script>
